package com.ruoyi.project.app.util;

import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static org.junit.Assert.assertEquals;

public class KxtSmsUtil {
    private static String account = "mt6327";//mt3569
    private static String password = "mgvpx";

    public static JSONObject sendSms(String mobiles,String code,String type) {
        int respCode=0;
        long t1 = System.currentTimeMillis();
        long totaltime = 0;
//        String mobiles = "15023286425";
        String smscontent="";
        if(type.equals("1")){
            smscontent = "您的本次登录校验码为:"+code+"(3分钟内有效),请勿泄漏给他人。如非本人操作，请忽略本条消息！";
        }else if(type.equals("2")){
            smscontent = "你好，您正在进行天美贷实名认证，请确保该操作是您本人进行，您的校验码是:"+code+",该校验码3分钟内有效。";
        }
        HttpURLConnection connection = null;
        JSONObject jsonOs=new JSONObject();
        try {
            String url = UrlConst.sendSms;
            URL postUrl = new URL(url);
            // 打开连接
            long totalt1 = System.currentTimeMillis();
            connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.connect();
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            String timestamp = Long.toString(System.currentTimeMillis());
            StringBuffer access_token = new StringBuffer();
            access_token.append(timestamp).append(password);
            String access_tokenS = MD5.md5(access_token.toString());

            StringBuffer s1 = new StringBuffer();
            s1.append("account=").append(account).append("&timestamp=")
                    .append(timestamp);
            s1.append("&access_token=").append(access_tokenS)
                    .append("&receiver=").append(mobiles)
                    .append("&smscontent=")
                    //.append(Base64.encode(smscontent.getBytes("GBK")))
                    .append(java.net.URLEncoder.encode(smscontent,"UTF-8"))
                    .append("&extcode=");
            out.writeBytes(s1.toString());
            out.flush();
            out.close();
            long totalt2 = System.currentTimeMillis();
            totaltime = totaltime + (totalt2 - totalt1);
            System.out.println(connection.getResponseCode()
                    + "-----------------");
            respCode=connection.getResponseCode();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
            String lines =reader.readLine();

            if(lines!=null){
                jsonOs = JSONObject.parseObject(lines);
            }
//            while ((lines = reader.readLine()) != null) {
//                for (Object setkeyS : jsonOs.keySet()) {
//                    System.out.println(setkeyS + ":" + jsonOs.get(setkeyS));
//                }
//            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("耗时......" + totaltime);
        System.out.println("耗时,包括取连接......" + (t2 - t1));
        assertEquals(respCode, 200);
        return jsonOs;
    }

    public static JSONObject sendSms(String mobiles,String content) {
        int respCode=0;
        long t1 = System.currentTimeMillis();
        long totaltime = 0;
//        String mobiles = "15023286425";
        String smscontent=content;
        HttpURLConnection connection = null;
        JSONObject jsonOs=new JSONObject();
        try {
            String url = UrlConst.sendSms;
            URL postUrl = new URL(url);
            // 打开连接
            long totalt1 = System.currentTimeMillis();
            connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            connection.connect();
            DataOutputStream out = new DataOutputStream(
                    connection.getOutputStream());
            String timestamp = Long.toString(System.currentTimeMillis());
            StringBuffer access_token = new StringBuffer();
            access_token.append(timestamp).append(password);
            String access_tokenS = MD5.md5(access_token.toString());

            StringBuffer s1 = new StringBuffer();
            s1.append("account=").append(account).append("&timestamp=")
                    .append(timestamp);
            s1.append("&access_token=").append(access_tokenS)
                    .append("&receiver=").append(mobiles)
                    .append("&smscontent=")
                    //.append(Base64.encode(smscontent.getBytes("GBK")))
                    .append(java.net.URLEncoder.encode(smscontent,"UTF-8"))
                    .append("&extcode=");
            out.writeBytes(s1.toString());
            out.flush();
            out.close();
            long totalt2 = System.currentTimeMillis();
            totaltime = totaltime + (totalt2 - totalt1);
            System.out.println(connection.getResponseCode()
                    + "-----------------");
            respCode=connection.getResponseCode();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
            String lines =reader.readLine();

            if(lines!=null){
                jsonOs = JSONObject.parseObject(lines);
            }
//            while ((lines = reader.readLine()) != null) {
//                for (Object setkeyS : jsonOs.keySet()) {
//                    System.out.println(setkeyS + ":" + jsonOs.get(setkeyS));
//                }
//            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        long t2 = System.currentTimeMillis();
        System.out.println("耗时......" + totaltime);
        System.out.println("耗时,包括取连接......" + (t2 - t1));
        assertEquals(respCode, 200);
        return jsonOs;
    }
}
